package com.lijz.log;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

import java.io.Serializable;
import java.util.Date;
import java.util.Map;

//@Document(collection = "demoTest_log")
public class SysLog implements Serializable {
	private static final long serialVersionUID = 1L;
	@Indexed(name = "logid")
	private Long logId;           //日志主键
	private String type;            //日志类型
	private String title;           //日志模块
	private String remoteAddr;          //请求地址
	private String requestUri;          //URI
	private String method;          //请求方式
	private String params;          //提交参数
	private String exception;           //异常
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date operateDate;           //开始时间
	private Long timeout;         //执行时间
	private String userId;          //用户ID
	private String resultParams;//返回参数
	private String token;    //token值

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Long getLogId() {
		return logId;
	}

	public void setLogId(Long logId) {
		this.logId = logId;
	}

	public String getResultParams() {
		return resultParams;
	}

	public void setResultParams(String resultParams) {
		this.resultParams = resultParams;
	}

	public String getType() {
		return StringUtils.isBlank(type) ? type : type.trim();
	}

	public void setType(String type) {
		this.type = type;
	}


	public String getTitle() {
		return StringUtils.isBlank(title) ? title : title.trim();
	}

	public void setTitle(String title) {
		this.title = title;
	}


	public String getRemoteAddr() {
		return StringUtils.isBlank(remoteAddr) ? remoteAddr : remoteAddr.trim();
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}


	public String getRequestUri() {
		return StringUtils.isBlank(requestUri) ? requestUri : requestUri.trim();
	}

	public void setRequestUri(String requestUri) {
		this.requestUri = requestUri;
	}


	public String getMethod() {
		return StringUtils.isBlank(method) ? method : method.trim();
	}

	public void setMethod(String method) {
		this.method = method;
	}


	public String getParams() {
		return StringUtils.isBlank(params) ? params : params.trim();
	}

	public void setParams(String params) {
		this.params = params;
	}

	/**
	 * 设置请求参数
	 *
	 * @param paramMap
	 */
	public void setMapToParams(Map<String, String[]> paramMap) {
		if (paramMap == null) {
			return;
		}
		StringBuilder params = new StringBuilder();
		for (Map.Entry<String, String[]> param : ((Map<String, String[]>) paramMap).entrySet()) {
			params.append(("".equals(params.toString()) ? "" : "&") + param.getKey() + "=");
			String paramValue = (param.getValue() != null && param.getValue().length > 0 ? param.getValue()[0] : "");
//			params.append(StringUtils.abbr(StringUtils.endsWithIgnoreCase(param.getKey(), "password") ? "" : paramValue, 100));
		}
		this.params = params.toString();
	}


	public String getException() {
		return StringUtils.isBlank(exception) ? exception : exception.trim();
	}

	public void setException(String exception) {
		this.exception = exception;
	}


	public Date getOperateDate() {
		return operateDate;
	}

	public void setOperateDate(Date operateDate) {
		this.operateDate = operateDate;
	}


	public Long getTimeout() {
		return timeout;
	}

	public void setTimeout(Long timeout) {
		this.timeout = timeout;
	}


	public String getUserId() {
		return StringUtils.isBlank(userId) ? userId : userId.trim();
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}
}
